package com.lsl.test;

import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author lishuailong
 * @create 2020-07-16 16:41
 */
public class test2 {

    @Test
    public void test1() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        Class<Person> aClass = Person.class;

        Person person = aClass.newInstance();
        person.setAge(20);
        System.out.println(person);

        Field age = aClass.getDeclaredField("age");

        //获取对象的属性值
        age.setAccessible(false);
        Object o = age.get(aClass);
        System.out.println(o);

    }
    @Test
    public void test2() throws IllegalAccessException, InstantiationException, NoSuchFieldException {
        Class<Person> aClass = Person.class;

        Person person = aClass.newInstance();

        //设置对象属性
        Field name = aClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(person,"lsl");

        //获取对象属性值
        Object o = name.get(person);
        System.out.println("o = " + o);

        System.out.println(person);

    }
    @Test
    public void test3() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<Person> aClass = Person.class;

        Person person = aClass.newInstance();
        person.setName("lsl");
        person.setAge(23);

        Method show = aClass.getDeclaredMethod("show",String.class,Integer.class);
        show.setAccessible(true);
        show.invoke(person,"李帅龙",23);



    }

    @Test
    public void test4(){

    }



}
